package com.henu.xyh;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.henu.R;
import com.henu.xyh.entity.Express;
import com.henu.yizhiya.entity.Person;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class PublishActivity extends AppCompatActivity {

    private EditText edittext;
    private ImageButton publish_return;
    private ImageButton publish_submit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish);
        edittext= (EditText) findViewById(R.id.edit_pub);
        publish_return= (ImageButton) findViewById(R.id.public_return);
        publish_submit= (ImageButton) findViewById(R.id.publish_submit);
        publish_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        publish_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String content=edittext.getText().toString().trim();
                if(content==null||content.equals(""))
                {
                    Toast.makeText(PublishActivity.this, "请输入内容！", Toast.LENGTH_SHORT).show();
                }
                else {
                    final Express express=new Express();
                    Person person = BmobUser.getCurrentUser(Person.class);
                    express.setUser_Id(person);
                    express.setExpress_Message(content);
                    express.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            if (e==null) {
                                Toast.makeText(PublishActivity.this, "发表成功。", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                            else {
                                Toast.makeText(PublishActivity.this, "评论失败！", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            }
        });
    }
}
